package com.zhentao.controller;

import com.zhentao.pojo.Doctor;
import com.zhentao.pojo.User;
import com.zhentao.pojo.perms.Pages;
import com.zhentao.service.IDoctorService;
import com.zhentao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mp
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController {
@Autowired
    IDoctorService service;
@RequestMapping("list")
    public Result list(@RequestBody Pages pages){
    return service.list(pages);
}
@RequestMapping("add")
    public Result add(@RequestBody Doctor doctor){
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    User user = (User) authentication.getPrincipal();
    doctor.setCreateTime(new Date());
    doctor.setCreateBy(user.getUsername());
    boolean save = service.save(doctor);
    return save?Result.OK(save):Result.ERROR();
}
@RequestMapping("upd")
    public Result upd(@RequestBody Doctor doctor){
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    User user = (User) authentication.getPrincipal();
    doctor.setUpdateTime(new Date());
    doctor.setUpdateBy(user.getUsername());
    doctor.setStatus(1);
    boolean b = service.updateById(doctor);
    return b?Result.OK(b):Result.ERROR();
}
    @RequestMapping("upd2")
    public Result upd2(@RequestBody Doctor doctor){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        doctor.setUpdateTime(new Date());
        doctor.setUpdateBy(user.getUsername());
        doctor.setStatus(0);
        boolean b = service.updateById(doctor);
        return b?Result.OK(b):Result.ERROR();
    }
}
